import {
  loginDySms,
  sendCodeBtn
} from '../../../api/login'
const handleAuthorize = require('../../../utils/handleAuthorize')
const app = getApp()
Page({
  data: {
    codetip: '获取验证码',
    isabled: true, //获取验证码防止点击
    isGou: true, //是否勾选协议
    smsCode: '', //短信验证码
    dyCode: '', //抖音code
    phone: '', //电话号码
    linkJump: ''
  },

  onLoad: function (options) {
    if (!handleAuthorize.isEmpty(options.linkJump)) {
      this.setData({
        linkJump: options.linkJump
      })
    }
  },
  onShow: function (e) {
    this.getCode()
  },
  // 获取短信
  getSmsCode(e) {
    this.setData({
      smsCode: e.detail.value
    });
  },
  // 获取手机号码
  getPhone(e) {
    this.setData({
      phone: e.detail.value
    });
  },
  check() {
    // this.setData({
    //   isGou: !this.data.isGou
    // })
    const popup = this.selectComponent('#popup');
    popup.open();
  },
  close(e) {
    console.log('e', e)
  },
  // 不同意协议
  noAgreeWith() {
    this.setData({
      isGou: false
    })
    this.selectComponent('#popup').close()
  },
  // 同意协议
  agreeWith() {
    this.setData({
      isGou: true
    })
    this.selectComponent('#popup').close()
  },
  // 跳转协议
  jumpAgreement(e) {
    var title = ''
    var type = e.currentTarget.dataset.type
    if (type == 1) {
      title = '神椰用户服务协议'
    } else {
      title = '神椰隐私保护协议'
    }
    tt.navigateTo({
      url: `/pages/agreement/agreement?type=${type}&title=${title}`,
    });
  },
  // 获取code
  getCode() {
    let that = this;
    tt.login({
      success: function (res) {
        if (res.code) {
          // console.log('res.code',res.code)
          that.data.dyCode = res.code
        } else {
          console.log('获取code失败', res.errMsg)
        }
      }
    })
  },
  async getLoginDySms() {
    console.log(this.data)
    if (handleAuthorize.isEmpty(this.data.phone)) {
      tt.showToast({
        title: '请输入手机号码',
        icon: 'fail'
      });
      return false;
    } else if (handleAuthorize.isEmpty(this.data.smsCode)) {
      tt.showToast({
        title: '请输入手机验证码',
        icon: 'fail'
      });
      return false;
    } else if (!this.data.isGou) {
      tt.showToast({
        title: '请勾选协议',
        icon: 'fail'
      });
      return false;
    }
    var data = {
      phone: this.data.phone,
      code: this.data.smsCode,
      dyCode: this.data.dyCode,
      source: 3,
    }
    // 开发appid 测试删除掉 
    data.appId = app.globalData.appId
    data.loginType = app.globalData.loginType

    tt.showLoading({
      title: '登录中'
    })
    try {
      const res = await loginDySms(data);
      // console.log('login_res', res)
      tt.hideLoading();
      tt.setStorageSync('token', res.result);
      tt.showToast({
        title: '登录成功',
        icon: 'success'
      });
      // 判断是否有链接转跳
      if (!handleAuthorize.isEmpty(this.data.linkJump) && !handleAuthorize.isEmpty(tt.getStorageSync('linkJump'))) {
        setTimeout(() => {
          var curRoute = tt.getStorageSync('linkJump')
          // 防止微信静默 登录url 出错
          if (curRoute.indexOf('/pages/login/login') > -1) {
            curRoute = '/pages/index/index'
          }
          // 页面跳转时 传参(过长) JSON.parse(decodeURIComponent(this.linkJump))
          if(curRoute.indexOf('/pages/index/index') > -1 || curRoute.indexOf('/pages/my/my') > -1 || curRoute.indexOf('/pages/integralTask/integralTask') > -1){
            tt.reLaunch({
              url: curRoute
            })
          }else{
            tt.redirectTo({
              url: curRoute
            })
          }
        }, 800)
      } else {
        setTimeout(() => {
          tt.reLaunch({
            url: '/pages/index/index'
          })
        }, 800)
      }
    } catch {

    } finally {

    }
  },
  // 点击获取验证码
  async getPhoneCode() {
    try {
      if (handleAuthorize.isEmpty(this.data.phone)) {
        tt.showToast({
          title: '请输入手机号码',
          icon: 'fail'
        });
        return false;
      } else if (!this.data.isGou) {
        tt.showToast({
          title: '请勾选协议',
          icon: 'fail'
        });
        return false;
      }
      // 控制倒计时期间不可再点击
      if (this.data.isabled == false) {
        return
      }
      const res = await sendCodeBtn(this.data.phone);
      tt.showToast({
        icon: 'none',
        title: '发送成功,请注意查看短信'
      })
      this.setData({
        isabled: false
      })
      let count = 60
      let timer = setInterval(() => {
        count--
        if (count == 0) {
          this.setData({
            codetip: '重新获取',
            isabled: true
          })
          clearInterval(timer)
        } else {
          this.setData({
            codetip: count + 's'
          })
        }
      }, 1000)
    } catch (error) {

    }

  },
  //切换登录方式
  jumpLogin(){
    tt.navigateBack({
      delta: 1,
    });
  },
  // 暂不登录
  jumpHome() {
    tt.reLaunch({
      url: '/pages/index/index'
    })
  },
  // 勾选协议
  checkTheAgreement(e) {
    // var type = e.currentTarget.dataset.type
    this.setData({
      isGou: !this.data.isGou
    })
  }
})